package jee.boot.module.config;

import jee.boot.module.act.dao.ViewDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 自动初始化工作流相关的视图,如果有就忽略,没有则新建
 * 
 * @author zhilei.wang
 *
 */
@Component
public class InitActivitiView implements ApplicationRunner {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	public ViewDao dao;
	@Value("${spring.schema:}")
	private String schema;
	private List<String> view = new ArrayList<String>() {
		private static final long serialVersionUID = 1L;
		{
			add("v_act_ru_identitylink");// 运行时身份信息关联
			add("v_act_ru_variable");// 运行时流程变量
		}
	};

	@Override
	public void run(ApplicationArguments args) throws Exception {
		logger.info("工作流视图初始化");
		if (!view.isEmpty()) {
			for (String viewName : view) {
				Map<String,Object>params=new HashMap<>();
				params.put("viewName", viewName);
				params.put("schema", schema);
				Integer existView = dao.existView(params);
				if (existView > 0) {
					continue;
				}
				try {
					dao.createView(params);
					logger.info("create view '" +schema+"."+ viewName+"' success");
				} catch (Exception e) {
					logger.error("create view '" +schema+"."+ viewName+ "' error", e);
				}
			}

		}
	}

}
